<?php

namespace App\Admin\Controllers;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use App\Models\Specification;
use Encore\Admin\Layout\Content;
use App\Models\SpecificationItem;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;

class SpecificationItemsController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('规格值列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('规格值')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑规格值')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('添加规格值')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new SpecificationItem);

        $grid->id('Id');
        $grid->specification_id('Specification id');
        $grid->column('spec.title','所属规格')->sortable();
        $grid->item('规格值名');
        $grid->value('规格值');
        $grid->sort('排序')->sortable();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(SpecificationItem::findOrFail($id));

        $show->id('Id');
        $show->specification_id('规格ID');
        $show->item('规格值名');
        $show->value('规格值');
        $show->sort('排序');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new SpecificationItem);

        $form->select('specification_id', '选择规格')->options(Specification::pluck('name','id'))->rules('required');
        $form->text('item', '规格值名')->rules('required');
        $form->text('value', '规格值')->rules('required')->help('颜色值可以通过代码表查看：<a target="_blank" href="https://www.sioe.cn/yingyong/yanse-rgb-16/">代码查询</a>');
        $form->number('sort', '排序')->default(0);
        $form->tools(function (Form\Tools $tools) {
            // 去掉`查看`按钮
            $tools->disableView();
            $tools->add('<a  href="javascript:history.back(-1);" class="btn btn-sm btn-primary"><i class="fa fa-reply"></i>&nbsp;&nbsp;返回</a>');
        });
        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
        });
        return $form;
    }
}
